home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / core.c,v < prev    next >
Encoding:
Text File  |  1991-07-03  |  14.4 KB  |  628 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     91.07.02.22.55.03;  author jhh;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     90.12.09.22.35.55;  author rab;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     90.11.09.17.04.12;  author rab;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @previous changes don't include Mips
  32. @
  33. text
  34. @/* Work with core dump and executable files, for GDB.
  35.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  36.  
  37. This file is part of GDB.
  38.  
  39. GDB is free software; you can redistribute it and/or modify
  40. it under the terms of the GNU General Public License as published by
  41. the Free Software Foundation; either version 1, or (at your option)
  42. any later version.
  43.  
  44. GDB is distributed in the hope that it will be useful,
  45. but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  47. GNU General Public License for more details.
  48.  
  49. You should have received a copy of the GNU General Public License
  50. along with GDB; see the file COPYING.  If not, write to
  51. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  52.  
  53. #include <stdio.h>
  54. #include "defs.h"
  55. #include "param.h"
  56. #include "frame.h"  /* required by inferior.h */
  57. #include "inferior.h"
  58.  
  59. #ifdef USG
  60. #include <sys/types.h>
  61. #include <fcntl.h>
  62. #endif
  63.  
  64. #ifdef COFF_ENCAPSULATE
  65. #include "a.out.encap.h"
  66. #else
  67. #include <a.out.h>
  68. #endif
  69. #ifndef N_MAGIC
  70. #ifdef COFF_FORMAT
  71. #define N_MAGIC(exec) ((exec).magic)
  72. #else
  73. #define N_MAGIC(exec) ((exec).a_magic)
  74. #endif
  75. #endif
  76. #include <signal.h>
  77. #include <sys/param.h>
  78. #include <sys/dir.h>
  79. #include <sys/file.h>
  80. #include <sys/stat.h>
  81.  
  82. #ifdef UMAX_CORE
  83. #include <sys/ptrace.h>
  84. #else
  85. #include <sys/user.h>
  86. #endif
  87.  
  88. #ifndef N_TXTADDR
  89. #define N_TXTADDR(hdr) 0
  90. #endif /* no N_TXTADDR */
  91.  
  92. #ifndef N_DATADDR
  93. #define N_DATADDR(hdr) hdr.a_text
  94. #endif /* no N_DATADDR */
  95.  
  96. #ifndef COFF_FORMAT
  97. #ifndef AOUTHDR
  98. #define AOUTHDR        struct exec
  99. #endif
  100. #endif
  101.  
  102. extern char *sys_siglist[];
  103.  
  104. extern core_file_command (), exec_file_command ();
  105.  
  106. /* Hook for `exec_file_command' command to call.  */
  107.  
  108. void (*exec_file_display_hook) ();
  109.    
  110. /* File names of core file and executable file.  */
  111.  
  112. char *corefile;
  113. char *execfile;
  114.  
  115. /* Descriptors on which core file and executable file are open.
  116.    Note that the execchan is closed when an inferior is created
  117.    and reopened if the inferior dies or is killed.  */
  118.  
  119. int corechan;
  120. int execchan;
  121.  
  122. /* Last modification time of executable file.
  123.    Also used in source.c to compare against mtime of a source file.  */
  124.  
  125. int exec_mtime;
  126.  
  127. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  128.  
  129. CORE_ADDR data_start;
  130. CORE_ADDR data_end;
  131. CORE_ADDR stack_start;
  132. CORE_ADDR stack_end;
  133.  
  134. #if defined (REG_STACK_SEGMENT)
  135. /* Start and end of the register stack segment.  */
  136. CORE_ADDR reg_stack_start;
  137. CORE_ADDR reg_stack_end;
  138. #endif /* REG_STACK_SEGMENT */
  139.  
  140. /* Virtual addresses of bounds of two areas of memory in the exec file.
  141.    Note that the data area in the exec file is used only when there is no core file.  */
  142.  
  143. CORE_ADDR text_start;
  144. CORE_ADDR text_end;
  145.  
  146. CORE_ADDR exec_data_start;
  147. CORE_ADDR exec_data_end;
  148.  
  149. /* Offset within executable file of start of text area data.  */
  150.  
  151. int text_offset;
  152.  
  153. /* Offset within executable file of start of data area data.  */
  154.  
  155. int exec_data_offset;
  156.  
  157. /* Offset within core file of start of data area data.  */
  158.  
  159. int data_offset;
  160.  
  161. /* Offset within core file of start of stack area data.  */
  162.  
  163. int stack_offset;
  164.   
  165. #ifdef COFF_FORMAT
  166. /* various coff data structures */
  167.  
  168. FILHDR file_hdr;
  169. SCNHDR text_hdr;
  170. SCNHDR data_hdr;
  171.  
  172. #endif /* not COFF_FORMAT */
  173.  
  174. /* a.out header saved in core file.  */
  175.   
  176. AOUTHDR core_aouthdr;
  177.  
  178. /* a.out header of exec file.  */
  179.  
  180. AOUTHDR exec_aouthdr;
  181.  
  182. void validate_files ();
  183. unsigned int register_addr ();
  184.  
  185. /* Call this to specify the hook for exec_file_command to call back.
  186.    This is called from the x-window display code.  */
  187.  
  188. void
  189. specify_exec_file_hook (hook)
  190.      void (*hook) ();
  191. {
  192.   exec_file_display_hook = hook;
  193. }
  194.  
  195. /* The exec file must be closed before running an inferior.
  196.    If it is needed again after the inferior dies, it must
  197.    be reopened.  */
  198.  
  199. void
  200. close_exec_file ()
  201. {
  202.   if (execchan >= 0)
  203.     close (execchan);
  204.   execchan = -1;
  205. }
  206.  
  207. void
  208. reopen_exec_file ()
  209. {
  210.   if (execchan < 0 && execfile != 0)
  211.     {
  212.       char *filename = concat (execfile, "", "");
  213.       exec_file_command (filename, 0);
  214.       free (filename);
  215.     }
  216. }
  217.  
  218. /* If we have both a core file and an exec file,
  219.    print a warning if they don't go together.
  220.    This should really check that the core file came
  221.    from that exec file, but I don't know how to do it.  */
  222.  
  223. void
  224. validate_files ()
  225. {
  226.   if (execfile != 0 && corefile != 0)
  227.     {
  228.       struct stat st_core;
  229.  
  230.       if (fstat (corechan, &st_core) < 0)
  231.     /* It might be a good idea to print an error message.
  232.        On the other hand, if the user tries to *do* anything with
  233.        the core file, (s)he'll find out soon enough.  */
  234.     return;
  235.  
  236.       if (N_MAGIC (core_aouthdr) != 0
  237. #if (defined(sprite) && !defined(mips))
  238.      /*
  239.       * The core_aouthdr in Sprite as generated by gcore is 
  240.       * only a guess at the real exec header. The only thing that
  241.       * should always be right is the text size.
  242.       */
  243.       && (core_aouthdr.a_text != exec_aouthdr.a_text))
  244. #else
  245.           && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  246. #endif
  247.     printf ("Warning: core file does not match specified executable file.\n");
  248.       else if (exec_mtime > st_core.st_mtime)
  249.     printf ("Warning: exec file is newer than core file.\n");
  250.     }
  251. }
  252.  
  253. /* Return the name of the executable file as a string.
  254.    ERR nonzero means get error if there is none specified;
  255.    otherwise return 0 in that case.  */
  256.  
  257. char *
  258. get_exec_file (err)
  259.      int err;
  260. {
  261.   if (err && execfile == 0)
  262.     error ("No executable file specified.\n\
  263. Use the \"exec-file\" and \"symbol-file\" commands.");
  264.   return execfile;
  265. }
  266.  
  267. int
  268. have_core_file_p ()
  269. {
  270.   return corefile != 0;
  271. }
  272.  
  273. static void
  274. files_info ()
  275. {
  276.   char *symfile;
  277.   extern char *get_sym_file ();
  278.  
  279.   if (execfile)
  280.     printf ("Executable file \"%s\".\n", execfile);
  281.   else
  282.     printf ("No executable file\n");
  283.   if (corefile == 0)
  284.     printf ("No core dump file\n");
  285.   else
  286.     printf ("Core dump file \"%s\".\n", corefile);
  287.  
  288.   if (have_inferior_p ())
  289.     printf ("Using the running image of the program, rather than these files.\n");
  290.  
  291.   symfile = get_sym_file ();
  292.   if (symfile != 0)
  293.     printf ("Symbols from \"%s\".\n", symfile);
  294.  
  295. #ifdef FILES_INFO_HOOK
  296.   if (FILES_INFO_HOOK ())
  297.     return;
  298. #endif
  299.  
  300.   if (! have_inferior_p ())
  301.     {
  302.       if (execfile)
  303.     {
  304.       printf ("Text segment in executable from 0x%x to 0x%x.\n",
  305.           text_start, text_end);
  306.       printf ("Data segment in executable from 0x%x to 0x%x.\n",
  307.           exec_data_start, exec_data_end);
  308.       if (corefile)
  309.         printf ("(But since we have a core file, we're using...)\n");
  310.     }
  311.       if (corefile)
  312.     {
  313.       printf ("Data segment in core file from 0x%x to 0x%x.\n",
  314.           data_start, data_end);
  315.       printf ("Stack segment in core file from 0x%x to 0x%x.\n",
  316.           stack_start, stack_end);
  317.     }
  318.     }
  319. }
  320.  
  321. /* Read "memory data" from core file and/or executable file.
  322.    Returns zero if successful, 1 if xfer_core_file failed, errno value if
  323.    ptrace failed. */
  324.  
  325. int
  326. read_memory (memaddr, myaddr, len)
  327.      CORE_ADDR memaddr;
  328.      char *myaddr;
  329.      int len;
  330. {
  331.   if (len == 0)
  332.     return 0;
  333.  
  334.   if (have_inferior_p ())
  335.     {
  336.       if (remote_debugging)
  337.     return remote_read_inferior_memory (memaddr, myaddr, len);
  338.       else
  339.     return read_inferior_memory (memaddr, myaddr, len);
  340.     }
  341.   else
  342.       return xfer_core_file (memaddr, myaddr, len);
  343. }
  344.  
  345. /* Write LEN bytes of data starting at address MYADDR
  346.    into debugged program memory at address MEMADDR.
  347.    Returns zero if successful, or an errno value if ptrace failed.  */
  348.  
  349. int
  350. write_memory (memaddr, myaddr, len)
  351.      CORE_ADDR memaddr;
  352.      char *myaddr;
  353.      int len;
  354. {
  355.   if (have_inferior_p ())
  356.     {
  357.       if (remote_debugging)
  358.     return remote_write_inferior_memory (memaddr, myaddr, len);
  359.       else
  360.     return write_inferior_memory (memaddr, myaddr, len);
  361.     }
  362.   else
  363.     error ("Can write memory only when program being debugged is running.");
  364. }
  365.  
  366. #ifndef XFER_CORE_FILE
  367. /* Read from the program's memory (except for inferior processes).
  368.    This function is misnamed, since it only reads, never writes; and
  369.    since it will use the core file and/or executable file as necessary.
  370.  
  371.    It should be extended to write as well as read, FIXME, for patching files.
  372.  
  373.    Return 0 if address could be read, 1 if not. */
  374.  
  375. int
  376. xfer_core_file (memaddr, myaddr, len)
  377.      CORE_ADDR memaddr;
  378.      char *myaddr;
  379.      int len;
  380. {
  381.   register int i;
  382.   register int val;
  383.   int xferchan;
  384.   char **xferfile;
  385.   int fileptr;
  386.   int returnval = 0;
  387.  
  388.   while (len > 0)
  389.     {
  390.       xferfile = 0;
  391.       xferchan = 0;
  392.  
  393.       /* Determine which file the next bunch of addresses reside in,
  394.      and where in the file.  Set the file's read/write pointer
  395.      to point at the proper place for the desired address
  396.      and set xferfile and xferchan for the correct file.
  397.  
  398.      If desired address is nonexistent, leave them zero.
  399.  
  400.      i is set to the number of bytes that can be handled
  401.      along with the next address.
  402.  
  403.      We put the most likely tests first for efficiency.  */
  404.  
  405.       /* Note that if there is no core file
  406.      data_start and data_end are equal.  */
  407.       if (memaddr >= data_start && memaddr < data_end)
  408.     {
  409.       i = min (len, data_end - memaddr);
  410.       fileptr = memaddr - data_start + data_offset;
  411.       xferfile = &corefile;
  412.       xferchan = corechan;
  413.     }
  414.       /* Note that if there is no core file
  415.      stack_start and stack_end are equal.  */
  416.       else if (memaddr >= stack_start && memaddr < stack_end)
  417.     {
  418.       i = min (len, stack_end - memaddr);
  419.       fileptr = memaddr - stack_start + stack_offset;
  420.       xferfile = &corefile;
  421.       xferchan = corechan;
  422.     }
  423. #ifdef REG_STACK_SEGMENT
  424.       /* Pyramids have an extra segment in the virtual address space
  425.          for the (control) stack of register-window frames */
  426.       else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
  427.     {
  428.       i = min (len, reg_stack_end - memaddr);
  429.       fileptr = memaddr - reg_stack_start + reg_stack_offset;
  430.       xferfile = &corefile;
  431.       xferchan = corechan;
  432.     }
  433. #endif /* REG_STACK_SEGMENT */
  434.  
  435.       else if (corechan < 0
  436.            && memaddr >= exec_data_start && memaddr < exec_data_end)
  437.     {
  438.       i = min (len, exec_data_end - memaddr);
  439.       fileptr = memaddr - exec_data_start + exec_data_offset;
  440.       xferfile = &execfile;
  441.       xferchan = execchan;
  442.     }
  443.       else if (memaddr >= text_start && memaddr < text_end)
  444.     {
  445.       i = min (len, text_end - memaddr);
  446.       fileptr = memaddr - text_start + text_offset;
  447.       xferfile = &execfile;
  448.       xferchan = execchan;
  449.     }
  450.       else if (memaddr < text_start)
  451.     {
  452.       i = min (len, text_start - memaddr);
  453.     }
  454.       else if (memaddr >= text_end
  455.            && memaddr < (corechan >= 0? data_start : exec_data_start))
  456.     {
  457.       i = min (len, data_start - memaddr);
  458.     }
  459.       else if (corechan >= 0
  460.            && memaddr >= data_end && memaddr < stack_start)
  461.     {
  462.       i = min (len, stack_start - memaddr);
  463.     }
  464.       else if (corechan < 0 && memaddr >= exec_data_end)
  465.     {
  466.       /* Since there is nothing at higher addresses than data
  467.          (without a core file or an inferior, there is no
  468.          stack, set i to do the rest of the operation now.  */
  469.       i = len;
  470.     }
  471. #ifdef REG_STACK_SEGMENT
  472.       else if (memaddr >= reg_stack_end && reg_stack_end != 0)
  473.     {
  474.       i = min (len, reg_stack_start - memaddr);
  475.     }
  476.       else if (memaddr >= stack_end && memaddr < reg_stack_start)
  477. #else /* no REG_STACK_SEGMENT.  */
  478.       else if (memaddr >= stack_end && stack_end != 0)
  479. #endif /* no REG_STACK_SEGMENT.  */
  480.     {
  481.       /* Since there is nothing at higher addresses than
  482.          the stack, set i to do the rest of the operation now.  */
  483.       i = len;
  484.     }
  485.       else
  486.     {
  487.       /* Address did not classify into one of the known ranges.
  488.          This shouldn't happen; we catch the endpoints.  */
  489.       fatal ("Internal: Bad case logic in xfer_core_file.");
  490.     }
  491.  
  492.       /* Now we know which file to use.
  493.      Set up its pointer and transfer the data.  */
  494.       if (xferfile)
  495.     {
  496.       if (*xferfile == 0)
  497.         if (xferfile == &execfile)
  498.           error ("No program file to examine.");
  499.         else
  500.           error ("No core dump file or running program to examine.");
  501.       val = lseek (xferchan, fileptr, 0);
  502.       if (val < 0)
  503.         perror_with_name (*xferfile);
  504.       val = myread (xferchan, myaddr, i);
  505.       if (val < 0)
  506.         perror_with_name (*xferfile);
  507.     }
  508.       /* If this address is for nonexistent memory,
  509.      read zeros if reading, or do nothing if writing.
  510.      Actually, we never right.  */
  511.       else
  512.     {
  513.       bzero (myaddr, i);
  514.       returnval = 1;
  515.     }
  516.  
  517.       memaddr += i;
  518.       myaddr += i;
  519.       len -= i;
  520.     }
  521.   return returnval;
  522. }
  523. #endif /* XFER_CORE_FILE */
  524.  
  525. /* My replacement for the read system call.
  526.    Used like `read' but keeps going if `read' returns too soon.  */
  527.  
  528. int
  529. myread (desc, addr, len)
  530.      int desc;
  531.      char *addr;
  532.      int len;
  533. {
  534.   register int val;
  535.   int orglen = len;
  536.  
  537.   while (len > 0)
  538.     {
  539.       val = read (desc, addr, len);
  540.       if (val < 0)
  541.     return val;
  542.       if (val == 0)
  543.     return orglen - len;
  544.       len -= val;
  545.       addr += val;
  546.     }
  547.   return orglen;
  548. }
  549.  
  550. #ifdef REGISTER_U_ADDR
  551.  
  552. /* Return the address in the core dump or inferior of register REGNO.
  553.    BLOCKEND is the address of the end of the user structure.  */
  554.  
  555. unsigned int
  556. register_addr (regno, blockend)
  557.      int regno;
  558.      int blockend;
  559. {
  560.   int addr;
  561.  
  562.   if (regno < 0 || regno >= NUM_REGS)
  563.     error ("Invalid register number %d.", regno);
  564.  
  565.   REGISTER_U_ADDR (addr, blockend, regno);
  566.  
  567.   return addr;
  568. }
  569.  
  570. #endif /* REGISTER_U_ADDR */
  571.  
  572. void
  573. _initialize_core()
  574. {
  575.   corechan = -1;
  576.   execchan = -1;
  577.   corefile = 0;
  578.   execfile = 0;
  579.   exec_file_display_hook = 0;
  580.  
  581.   text_start = 0;
  582.   text_end = 0;
  583.   data_start = 0;
  584.   data_end = 0;
  585.   exec_data_start = 0;
  586.   exec_data_end = 0;
  587.   stack_start = STACK_END_ADDR;
  588.   stack_end = STACK_END_ADDR;
  589.  
  590. #if (defined(sprite) && !defined(mips))
  591.   add_com ("core-file", class_files, core_file_command,
  592.        "Use FILE as core dump for examining memory and registers.\n\
  593. No arg means have no core file.");
  594. #endif
  595.   add_com ("exec-file", class_files, exec_file_command,
  596.        "Use FILE as program for getting contents of pure memory.\n\
  597. If FILE cannot be found as specified, your execution directory path\n\
  598. is searched for a command of that name.\n\
  599. No arg means have no executable file.");
  600.   add_info ("files", files_info, "Names of files being debugged.");
  601. }
  602.  
  603. @
  604.  
  605.  
  606. 1.2
  607. log
  608. @Changes for Sprite. (Mike checking in for Bob.)
  609. @
  610. text
  611. @d204 1
  612. a204 1
  613. #ifdef sprite
  614. d557 1
  615. d561 1
  616. @
  617.  
  618.  
  619. 1.1
  620. log
  621. @Initial revision
  622. @
  623. text
  624. @d204 10
  625. a213 1
  626.       && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  627. @
  628.